package daemonPack

import (
	"github.com/astaxie/beego/logs"
	"github.com/gin-gonic/gin"
	"github.com/sevlyar/go-daemon"
	v1 "wonlicsys/api/v1"
)

func DaemonStart() error {
	//后台开启
	if v1.Conf.RunMode == "dev" {
		gin.SetMode(gin.DebugMode)
		logs.SetLevel(logs.LevelDebug)

		logs.SetLogFuncCall(true) // 输出log时能显示输出文件名和行号（非必须）
		//todo

		//	logs.SetLogger(logs.AdapterFile, `{"filename":"`+v1.Conf.LogPath+`"}`)
		//logs.SetLogger("console", `{"level":1}`)
	} else {
		gin.SetMode(gin.ReleaseMode)
		logs.SetLogger(logs.AdapterFile, `{"filename":"`+v1.Conf.LogPath+`"}`)
		logs.SetLevel(logs.LevelError)
		logs.SetLogFuncCall(true) // 输出log时能显示输出文件名和行号（非必须）
		cntxt := &daemon.Context{}
		d, err := cntxt.Reborn()
		if err != nil {
			logs.Error(err)
			return err
		}
		if d != nil {
			return err
		}
		defer cntxt.Release()
	}
	return nil
}
